package com.muyaxuan.mail.controller.customer;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.muyaxuan.mail.common.WebConstant;
import com.muyaxuan.mail.dao.UserDao;
import com.muyaxuan.mail.dto.User;
@RequestMapping("/customer")
@Controller
public class LoginController {
	
	@RequestMapping("/showLogin")
	public String showLogin(){
		
		return "login";
		
	}
	/**
	 * 用户登录方法
	 * @param userId
	 * @param password
	 * @param code
	 * @param request
	 * @param session
	 * @return Map<String,Object> 集合
	 */
	@ResponseBody
	@RequestMapping("/loginAjax")
	public Map<String,Object> login(String userId,String password,String code,HttpServletRequest request,HttpSession session){
		User user=null;
		UserDao userDao=null;
		Map<String,Object> map=new HashMap<>();
		
		String oldCode=(String)request.getSession().getAttribute("code");
		
        if(code==null||!oldCode.equals(code)){
        	map.put("status", 1);
        	map.put("tip", "验证码错误");
        }else{
        	user=new User();
        	user.setUserId(userId);
        	user.setPassword(password);
        	userDao=new UserDao();
        	user=userDao.getUserByUserIdAndPass(user);
        	if(user!=null){
        		session.setAttribute(WebConstant.SESSION_USER, user);
        		map.put("status", 0);
        	}else{
        		map.put("status", 3);
        		map.put("tip", "用户名或者密码错误");
        	}
        }
        return map;
	}
	/**
	 * 用户退出
	 * @param session
	 * @return String("home")
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.invalidate();
		//服务器端重定向
		//return "forward:/admin/goods/goodsList";
		//客户端重定向
		return "redirect:/customer/home";
	}
	
	
	
	
}
